package org.hospital.service.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hospital.entity.Bill;
import org.hospital.entity.BillDAO;
import org.hospital.entity.BillProject;
import org.hospital.entity.BillProjectDAO;
import org.hospital.entity.BillProjectId;
import org.hospital.entity.Client;
import org.hospital.entity.ClientDAO;
import org.hospital.entity.Clinic;
import org.hospital.entity.ClinicDAO;
import org.hospital.entity.Department;
import org.hospital.entity.DepartmentDAO;
import org.hospital.entity.Employee;
import org.hospital.entity.EmployeeDAO;
import org.hospital.entity.MedicalCard;
import org.hospital.entity.MedicalCardDAO;
import org.hospital.entity.MedicalRecord;
import org.hospital.entity.MedicalRecordDAO;
import org.hospital.entity.Project;
import org.hospital.entity.ProjectDAO;
import org.hospital.entity.TreatInfo;
import org.hospital.entity.TreatInfoDAO;
import org.hospital.service.BillService;
import org.hospital.util.DateUtil;
import org.hospital.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class BillServiceImpl implements BillService {

	@Autowired
	private BillDAO bDao;
	@Autowired
	private BillProjectDAO bpDao;
	@Autowired
	private MedicalCardDAO mcDao;
	@Autowired
	private MedicalRecordDAO mrDao;
	@Autowired
	private TreatInfoDAO tiDao;
	@Autowired
	private ProjectDAO pjDao;
	@Autowired
	private EmployeeDAO eDao;
	@Autowired
	private ClinicDAO cDao;
	@Autowired
	private ClientDAO clientDao;
	@Autowired
	private DepartmentDAO dpmDao;

	@Override
	public Bill getById(int billId) {
		return bDao.findById(billId);
	}

	@Override
	public Bill addEntity(Bill entity) {
		return bDao.merge(entity);
	}

	@Override
	public Bill saveEntity(Bill entity) {
		return bDao.merge(entity);
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	public Bill addTreatBill(Bill entity, JSONArray projects,int clientId,long startTime,String diseaseName) {
		
		Bill bill = bDao.merge(entity); //插入后的Bill数据对象
		
		for (int i = 0; i < projects.size(); i++) {
			JSONObject projectJO = projects.getJSONObject(i);
			
			int projectId = projectJO.getIntValue("projectId");
			Project project = pjDao.findById(projectId);
			int number = projectJO.getIntValue("number");
			double unitPrice = project.getUnitPrice();
			Timestamp deadline = new Timestamp(DateUtil.getShortDateLong(projectJO.getString("deadline")));
			
			// BillProject表
			BillProject bProject = new BillProject();
			bProject.setId(new BillProjectId(bill.getBillId(), projectId));
			bProject.setNumber(number);
			bProject.setPrice(unitPrice * number);
			bProject.setDoctorCost(project.getDoctorCost());
			if(projectJO.getString("extroInfo") != null) {
				bProject.setExtraInfo(projectJO.getString("extroInfo"));
			}
			bpDao.merge(bProject);
			
			// MedicalCard表   诊疗卡表
			MedicalCard mc = new MedicalCard();
			mc.setClientId(clientId);
			mc.setBillId(bill.getBillId());
			mc.setProjectId(project.getProjectId());
			mc.setUnitPrice(unitPrice);
			mc.setClinicCost(project.getClinicCost());
			mc.setExcutorCost(project.getExcutorCost());
			mc.setTotalNumber(number);
			mc.setRestNumber(number);
			mc.setStartTime(new Timestamp(startTime));
			mc.setDeadline(deadline);
			mc.setStatus(0);
			mcDao.merge(mc);
			
			//MedicalRecord 病历表
			MedicalRecord mr = new MedicalRecord();
			mr.setClientId(clientId);
			mr.setClinicId(bill.getClinicId());
			mr.setDatetime(new Timestamp(startTime));
			mr.setDepartmentId(bill.getDepartmentId());
			mr.setDiseaseName(diseaseName);
			mr.setEmployeeId(bill.getDoctorId());
			mrDao.merge(mr);
		}
		
		return bill;
	}
	
	@Override
	public Bill addFundBill(Bill entity, JSONArray projects) {
		Bill bill = bDao.merge(entity);
		
		// 插入billproject表
		JSONObject jo;
		BillProject bp;
		BillProjectId pk;
		for (int i = 0; i < projects.size(); i++) {
			jo = projects.getJSONObject(i);

			bp = new BillProject();
			pk = new BillProjectId(bill.getBillId(), jo.getInteger("projectId"));

			bp.setId(pk);
			bp.setNumber(0);
			bp.setPrice(jo.getDouble("cost"));
			bp.setDoctorCost(0.0);
			if(jo.get("markText") == null) {
				bp.setExtraInfo("null");
			}else {
				bp.setExtraInfo(jo.getString("markText"));
			}
			
			bpDao.merge(bp);
		}
		return bill;
	}
	
	
	@Override
	public JSONArray getDepartmentTreatmentProjectReport(List<Project> pjList,
			Department department, long startTime, long endTime) {
		// [{clinicId,clinicName,departmentId,departmentName,
		// * projectId,projectName
		// * ,sumIncome,sumSelfCost,sumOtherCost,profit}]
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < pjList.size(); i++) {
			Project pj = pjList.get(i);
			// 获取所有医生开单的BillProject
			List<BillProject> bpList = bDao.getBillProjectSummary(department.getDepartmentId(),
					pj.getProjectId(), startTime, endTime, 0);

			JSONObject resultJO = new JSONObject();
			double sumIncome = 0.0; // 治疗项目总收入
			double sumDoctorCost = 0.0; // 医生抽成总成本
			double sumSelfOperatorCost = 0.0; // 本科室执行者抽成成本
			double sumOtherOperatorCost = 0.0; // 其他部门执行者抽成成本(包含本院其他部门、分院其他部门)
			double sumOterClinicCost = 0.0; // 分院门诊抽成成本

			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				sumIncome += bp.getPrice();
				sumDoctorCost += (bp.getPrice() * bp.getDoctorCost() / 100);
			}

			// 获取本科室开单的执行者医生治疗信息
			List<TreatInfo> tiList = tiDao.getDepartmentTreatInfo(department.getDepartmentId(),
					pj.getProjectId(), startTime, endTime);
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				if (ti.getDepartmentId() == department.getDepartmentId()) {
					sumSelfOperatorCost += ti.getUnitPrice() * ti.getExcutorCost() / 100;
				} else {
					sumOtherOperatorCost += ti.getUnitPrice() * ti.getExcutorCost() / 100;
				}
				if (ti.getClinicId() != department.getClinicId()) {
					sumOterClinicCost += ti.getUnitPrice() * ti.getClinicCost() / 100;
				}
			}

			// 获取其他科室开的单本科室执行者执行的治疗信息
			List<TreatInfo> tiList2 = tiDao.getOtherTreatInfo(department.getDepartmentId(),
					pj.getProjectId(), startTime, endTime, 0);
			for (int j = 0; j < tiList2.size(); j++) {
				TreatInfo ti = tiList2.get(j);
				sumSelfOperatorCost += (ti.getUnitPrice() * ti.getExcutorCost() / 100);
			}

			double sumSelfCost = sumDoctorCost + sumSelfOperatorCost;// 科室自身总支出
			double sumOtherCost = sumOtherOperatorCost + sumOterClinicCost;// 分科室支出

			Clinic clinic = cDao.findById(department.getClinicId());
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("departmentId", department.getDepartmentId());
			resultJO.put("departmentName", department.getName());
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("cureName", pj.getName());
			resultJO.put("cureCost", DoubleUtil.parseDoubleTwo(sumIncome));
			resultJO.put("centralCost", DoubleUtil.parseDoubleTwo(sumSelfCost));
			resultJO.put("branchCost", DoubleUtil.parseDoubleTwo(sumOtherCost));
			resultJO.put("centeralIncome",
					DoubleUtil.parseDoubleTwo(sumIncome - sumSelfCost - sumOtherCost));

			resultJA.add(resultJO);
		}

		return resultJA;
	}

	@Override
	public JSONArray getDepartmentSelfCostDetail(Project pj, Department department, long startTime,
			long endTime) {
		// [{employeeId,employeeCode,type,name,number,costPrice}]
		// 获取科室所有开过单的医生
		List<Employee> doctorList = eDao.getTreatmentDoctor(department.getDepartmentId(),
				pj.getProjectId(), startTime, endTime, 0);
		// 获取科室所有执行治疗过的执行者
		List<Employee> excutorList = eDao.getTreatmentExcutor(department.getDepartmentId(),
				pj.getProjectId(), startTime, endTime, 0);

		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < doctorList.size(); i++) {
			Employee doctor = doctorList.get(i);
			JSONObject resultJO = new JSONObject();
			// 获取科室医生开单的所有信息
			List<BillProject> bpList = bDao.getDoctorSummaryInProject(department.getDepartmentId(),
					doctor, pj.getProjectId(), startTime, endTime, 0);
			double costPrice = 0.0;
			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				costPrice += (bp.getDoctorCost() * bp.getPrice() / 100);
			}
			resultJO.put("employeeId", doctor.getEmployeeId());
			resultJO.put("employeeCode", doctor.getEmployeeCode());
			resultJO.put("type", "开单医生");
			resultJO.put("name", doctor.getName());
			resultJO.put("number", bpList.size());
			resultJO.put("costPrice", DoubleUtil.parseDoubleTwo(costPrice));
			resultJA.add(resultJO);
		}

		for (int i = 0; i < excutorList.size(); i++) {
			Employee excutor = excutorList.get(i);
			JSONObject resultJO = new JSONObject();
			// 获取科室执行者治疗的所有信息
			List<TreatInfo> tiList = tiDao.getSelfExcutorTreatInfo(excutor,
					department.getDepartmentId(), pj.getProjectId(), startTime, endTime, 0);

			double costPrice = 0.0;
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				costPrice += (ti.getExcutorCost() * ti.getUnitPrice() / 100);
			}
			resultJO.put("employeeId", excutor.getEmployeeId());
			resultJO.put("employeeCode", excutor.getEmployeeCode());
			resultJO.put("type", "执行医生");
			resultJO.put("name", excutor.getName());
			resultJO.put("number", tiList.size());
			resultJO.put("costPrice", DoubleUtil.parseDoubleTwo(costPrice));
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	// [{clinicId,clinicName,clinicCost,employeeId,employeeCode,employeeName
	// ,employeeCost,number,totalCost}]
	@Override
	public JSONArray getDepartmentOtherCostDetail(Project pj, Department dpm, long startTime,
			long endTime) {
		JSONArray resultJA = new JSONArray();
		// TODO 默认执行者没有在两个不同的门诊执行治疗；
		// 获取所有其他部门、且不同门诊、执行过本部门开的治疗项目的诊执行者
		List<Employee> excutorList = eDao.getTreatmentOtherExcutor(dpm.getClinicId(),
				dpm.getDepartmentId(), pj.getProjectId(), startTime, endTime, 1);
		for (int i = 0; i < excutorList.size(); i++) {
			Employee excutor = excutorList.get(i);
			JSONObject resultJO = new JSONObject();
			// 获取其他门诊其他部门的执行者 执行信息
			List<TreatInfo> tiList = tiDao.getDepartmentOtherTreatInfo(excutor, dpm.getClinicId(),
					dpm.getDepartmentId(), pj.getProjectId(), startTime, endTime, 0);
			double clinicCost = 0.0; // 门诊支出
			double employeeCost = 0.0; // 执行医生支出
			double totalCost = 0.0; // 总支出
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				clinicCost += ti.getClinicCost() * ti.getUnitPrice() / 100;
				employeeCost += ti.getExcutorCost() * ti.getUnitPrice() / 100;
			}
			totalCost = clinicCost + employeeCost;
			Clinic clinic = cDao.findById(tiList.get(0).getClinicId());
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("clinicCost", DoubleUtil.parseDoubleTwo(clinicCost));
			resultJO.put("employeeId", excutor.getEmployeeId());
			resultJO.put("employeeCode", excutor.getEmployeeCode());
			resultJO.put("employeeName", excutor.getName());
			resultJO.put("employeeCost", DoubleUtil.parseDoubleTwo(employeeCost));
			resultJO.put("number", tiList.size());
			resultJO.put("totalCost", DoubleUtil.parseDoubleTwo(totalCost));
			resultJA.add(resultJO);
		}

		// 获取所有获取所有其他部门、且相同门诊、执行过本部门开的治疗项目的执行者
		List<Employee> excutorList2 = eDao.getTreatmentOtherExcutor(dpm.getClinicId(),
				dpm.getDepartmentId(), pj.getProjectId(), startTime, endTime, 0);
		for (int i = 0; i < excutorList2.size(); i++) {
			Employee excutor = excutorList2.get(i);
			JSONObject resultJO = new JSONObject();
			// 获取其他部门本门诊的执行者执行信息
			List<TreatInfo> tiList = tiDao.getDepartmentOtherTreatInfo(excutor, dpm.getClinicId(),
					dpm.getDepartmentId(), pj.getProjectId(), startTime, endTime, 1);
			double employeeCost = 0.0; // 执行医生支出
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				employeeCost += ti.getExcutorCost() * ti.getUnitPrice() / 100;
			}
			Clinic clinic = cDao.findById(dpm.getClinicId());
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("clinicCost", 0);
			resultJO.put("employeeId", excutor.getEmployeeId());
			resultJO.put("employeeCode", excutor.getEmployeeCode());
			resultJO.put("employeeName", excutor.getName());
			resultJO.put("employeeCost", DoubleUtil.parseDoubleTwo(employeeCost));
			resultJO.put("number", tiList.size());
			resultJO.put("totalCost", DoubleUtil.parseDoubleTwo(employeeCost));
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public JSONArray getDepartmentTreatmentDetail(Project pj, Department dpm, long startTime,
			long endTime) {
		JSONArray resultJA = new JSONArray();

		// 获取所有开过单的医生
		List<Employee> doctorList = eDao.getTreatmentDoctor(dpm.getDepartmentId(),
				pj.getProjectId(), startTime, endTime, 0);

		for (int i = 0; i < doctorList.size(); i++) {
			Employee doctor = doctorList.get(i);
			JSONObject resultJO = new JSONObject();
			List<BillProject> bpList = bDao.getDoctorSummaryInProject(dpm.getDepartmentId(),
					doctor, pj.getProjectId(), startTime, endTime, 0);
			double price = 0.0;
			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				price += bp.getPrice();
			}
			resultJO.put("departmentId", dpm.getDepartmentId());
			resultJO.put("departmentName", dpm.getName());
			resultJO.put("doctorId", doctor.getEmployeeId());
			resultJO.put("doctorCode", doctor.getEmployeeCode());
			resultJO.put("doctorName", doctor.getName());
			resultJO.put("price", DoubleUtil.parseDoubleTwo(price));
			resultJO.put("billNumber", bpList.size());
			resultJA.add(resultJO);
		}

		return resultJA;
	}
	
	@Override
	public JSONArray getClinicTreatmentProjectReport(List<Project> pjList, Clinic clinic,
			long startTime, long endTime) {
		JSONArray resultJA = new JSONArray();
		for (int i = 0; i < pjList.size(); i++) {
			Project project = (Project) pjList.get(i);
			JSONObject resultJO = new JSONObject();

			double sumIncome = 0.0; // 治疗项目总收入
			double sumOtherClinicIncome = 0.0; // 获取分院抽成成本
			double sumDoctorCost = 0.0; // 医生抽成总成本
			double sumOperatorCost = 0.0; // 本部执行者抽成成本
			double sumOtherOperatorCost = 0.0; // 分部执行者抽成成本
			double sumOtherClinicCost = 0.0; // 分部门诊抽成成本

			// 获取治疗项目开单详细信息
			List<BillProject> bpjList = bDao.getBillProjectSummary(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < bpjList.size(); j++) {
				BillProject billProject = (BillProject) bpjList.get(j);
				double income = billProject.getPrice();
				double doctorCost = (income * billProject.getDoctorCost()) / 100.0;
				sumIncome += income;
				sumDoctorCost += doctorCost;
			}

			// 获取本院开单的所有客户治疗信息的详细信息
			List<TreatInfo> tiList = tiDao.getClinicTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime);
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo treatInfo = tiList.get(j);
				if (treatInfo.getClinicId() == clinic.getClinicId()) {
					sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
					continue;
				}
				sumOtherOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
				sumOtherClinicCost += (treatInfo.getUnitPrice() * treatInfo.getClinicCost()) / 100.0;
			}

			// 获取本院 执行者执行其他门诊单据的治疗信息
			List<TreatInfo> tiList2 = tiDao.getOtherTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < tiList2.size(); j++) {
				TreatInfo ti = tiList2.get(j);
				sumOperatorCost += (ti.getUnitPrice() * ti.getExcutorCost() / 100);
				sumOtherClinicIncome += (ti.getUnitPrice() * ti.getClinicCost() / 100);
			}

			// 插入数据
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("hospitalName", clinic.getName());
			resultJO.put("projectId", project.getProjectId());
			resultJO.put("cureName", project.getName());
			resultJO.put("cureCost", DoubleUtil.parseDoubleTwo(sumIncome));
			resultJO.put("centralCost", DoubleUtil.parseDoubleTwo(sumDoctorCost + sumOperatorCost));
			resultJO.put("branchCost",
					DoubleUtil.parseDoubleTwo(sumOtherClinicCost + sumOtherOperatorCost));
			double sumProfit = sumIncome - sumDoctorCost - sumOperatorCost - sumOtherClinicCost
					- sumOtherOperatorCost + sumOtherClinicIncome;
			resultJO.put("centeralIncome", DoubleUtil.parseDoubleTwo(sumProfit));
			resultJO.put("branchIncome", DoubleUtil.parseDoubleTwo(sumOtherClinicIncome));
			resultJO.put("total", pjList.size());

			resultJA.add(resultJO);
		}

		return resultJA;
	}

	// [{employeeId,employeeCode,type,name,number,costPrice}]
	@Override
	public JSONArray getClinicSelfCostDetail(Project pj, Clinic clinic, long startTime, long endTime) {
		JSONArray resultJA = new JSONArray();
		// 获取所有开过单的医生
		List<Employee> doctorList = eDao.getTreatmentDoctor(clinic.getClinicId(),
				pj.getProjectId(), startTime, endTime, 1);
		// 获取本院所有执行治疗过的执行者
		List<Employee> excutorList = eDao.getTreatmentExcutor(clinic.getClinicId(),
				pj.getProjectId(), startTime, endTime, 1);

		// 遍历医生 获取每个医生汇总情况
		for (int i = 0; i < doctorList.size(); i++) {
			Employee doctor = doctorList.get(i);
			JSONObject resultJO = new JSONObject();
			List<BillProject> bpList = bDao.getDoctorSummaryInProject(clinic.getClinicId(), doctor,
					pj.getProjectId(), startTime, endTime, 1);

			double costPrice = 0.0;// 医生支出总额
			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				costPrice += bp.getDoctorCost() * bp.getPrice() / 100;
			}
			resultJO.put("employeeId", doctor.getEmployeeId());
			resultJO.put("employeeCode", doctor.getEmployeeCode());
			resultJO.put("type", "开单医生");
			resultJO.put("name", doctor.getName());
			resultJO.put("number", bpList.size());
			resultJO.put("costPrice", DoubleUtil.parseDoubleTwo(costPrice));
			resultJA.add(resultJO);
		}

		// 遍历执行者 获取每个执行者汇总情况
		for (int i = 0; i < excutorList.size(); i++) {
			Employee excutor = excutorList.get(i);
			JSONObject resultJO = new JSONObject();
			List<TreatInfo> tiList = tiDao.getSelfExcutorTreatInfo(excutor, clinic.getClinicId(),
					pj.getProjectId(), startTime, endTime, 1);
			double costPrice = 0.0; // 执行者支出总额
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				costPrice += ti.getUnitPrice() * ti.getExcutorCost() / 100;
			}
			resultJO.put("employeeId", excutor.getEmployeeId());
			resultJO.put("employeeCode", excutor.getEmployeeCode());
			resultJO.put("type", "执行医生");
			resultJO.put("name", excutor.getName());
			resultJO.put("number", tiList.size());
			resultJO.put("costPrice", DoubleUtil.parseDoubleTwo(costPrice));
			resultJA.add(resultJO);
		}

		return resultJA;
	}

	@Override
	public JSONArray getClinicOtherCostDetail(Project pj, Clinic clinic, long startTime,
			long endTime) {
		JSONArray resultJA = new JSONArray();

		// 获取分院所有执行治疗过的执行者
		List<Employee> excutorList = eDao.getTreatmentOtherExcutor(clinic.getClinicId(), 0,
				pj.getProjectId(), startTime, endTime, 1);
		System.out.println("BillServiceImpl.getClinicOtherCostDetail.excutorList : "
				+ JSON.toJSONString(excutorList));
		for (int i = 0; i < excutorList.size(); i++) {
			Employee excutor = excutorList.get(i);
			JSONObject resultJO = new JSONObject();
			List<TreatInfo> tiList = tiDao.getOtherExcutorTreatInfo(excutor, clinic.getClinicId(),
					pj.getProjectId(), startTime, endTime);
			System.out.println("BillServiceImpl.getClinicOtherCostDetail.tiList : "
					+ JSON.toJSONString(tiList));

			double clinicCost = 0.0; // 分院分成总额
			double employeeCost = 0.0; // 执行者分成总额
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo ti = tiList.get(j);
				clinicCost += ti.getUnitPrice() * ti.getClinicCost() / 100;
				employeeCost += ti.getUnitPrice() * ti.getExcutorCost() / 100;
			}

			Clinic excutorClinic = cDao.findById(tiList.get(0).getClinicId());
			// 格式化数据
			// TODO 默认excutor没有转院;
			resultJO.put("clinicId", excutorClinic.getClinicId());
			resultJO.put("clinicName", excutorClinic.getName());
			resultJO.put("clinicCost", DoubleUtil.parseDoubleTwo(clinicCost));
			resultJO.put("employeeId", excutor.getEmployeeId());
			resultJO.put("employeeCode", excutor.getEmployeeCode());
			resultJO.put("employeeName", excutor.getName());
			resultJO.put("employeeCost", DoubleUtil.parseDoubleTwo(employeeCost));
			resultJO.put("number", tiList.size());
			resultJO.put("totalCost", DoubleUtil.parseDoubleTwo(clinicCost + employeeCost));
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public JSONArray getClinicTreatmentDetail(Project pj, Clinic clinic, long startTime,
			long endTime) {
		JSONArray resultJA = new JSONArray();

		// 获取所有开过单的医生
		List<Employee> doctorList = eDao.getTreatmentDoctor(clinic.getClinicId(),
				pj.getProjectId(), startTime, endTime, 1);

		for (int i = 0; i < doctorList.size(); i++) {
			Employee doctor = doctorList.get(i);
			JSONObject resultJO = new JSONObject();
			List<BillProject> bpList = bDao.getDoctorSummaryInProject(clinic.getClinicId(), doctor,
					pj.getProjectId(), startTime, endTime, 1);
			double price = 0.0;
			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				price += bp.getPrice();
			}
			Department dpm = dpmDao.findById(doctor.getDepartmentId());
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("departmentId", dpm.getDepartmentId());
			resultJO.put("departmentName", dpm.getName());
			resultJO.put("doctorId", doctor.getEmployeeId());
			resultJO.put("doctorCode", doctor.getEmployeeCode());
			resultJO.put("doctorName", doctor.getName());
			resultJO.put("price", DoubleUtil.parseDoubleTwo(price));
			resultJO.put("billNumber", bpList.size());
			resultJA.add(resultJO);
		}

		return resultJA;
	}

	
	@Override
	public JSONArray getClinicFundProjectReport(Clinic clinic, long startTime, long endTime) {
		// [clinicId,clinicName,projectId,projectName,type,price]
		JSONArray resultJA = new JSONArray();
		// 获取[startTime,endTime]所有有效的(收入、支出)费用项目
		List<Project> pjFundList = pjDao.getClinicFundProject(clinic.getClinicId(), startTime,
				endTime);
		// System.out.println("getClinicFundProjectReport.pjFundList : "+JSON.toJSONString(pjFundList,true));
		for (int i = 0; i < pjFundList.size(); i++) {
			Project pj = pjFundList.get(i);
			JSONObject resultJO = new JSONObject();
			// 获取费用项目具体开单细节
			List<BillProject> bpList = bDao.getFundBPSummary(clinic.getClinicId(),
					pj.getProjectId(), startTime, endTime);
			// System.out.println("getClinicFundProjectReport.bpList : "+JSON.toJSONString(bpList,true));
			// 计算指定费用项目总价格
			double price = 0.0;
			for (int j = 0; j < bpList.size(); j++) {
				BillProject bp = bpList.get(j);
				price += bp.getPrice();
			}
			// 格式化数据
			resultJO.put("clinicId", clinic.getClinicId());
			resultJO.put("clinicName", clinic.getName());
			resultJO.put("projectId", pj.getProjectId());
			resultJO.put("projectName", pj.getName());
			resultJO.put("price", DoubleUtil.parseDoubleTwo(price));
			if (pj.getType() == 0) {
				resultJO.put("type", "收入");
			}
			if (pj.getType() == 1) {
				resultJO.put("type", "支出");
			}
			resultJA.add(resultJO);
		}
		// 获取[startTime,endTime]所有有效的治疗项目收入
		List<Project> pjTreatList = pjDao.getClinicTreatmentProject(clinic.getClinicId(),
				startTime, endTime);

		// 治疗项目获取的总利润=开单收入+门诊抽成收入-门诊抽成支出-执行者支出-医生支出
		double treatProfit = 0.0;
		for (int i = 0; i < pjTreatList.size(); i++) {
			Project project = (Project) pjTreatList.get(i);

			double sumIncome = 0.0; // 开单收入
			double sumOtherClinicIncome = 0.0; // 门诊抽成收入
			double sumDoctorCost = 0.0; // 医生支出
			double sumOperatorCost = 0.0; // 执行者支出
			double sumOtherClinicCost = 0.0; // 门诊抽成支出

			// 获取治疗项目开单详细信息
			List<BillProject> bpjList = bDao.getBillProjectSummary(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < bpjList.size(); j++) {
				BillProject billProject = (BillProject) bpjList.get(j);
				double income = billProject.getPrice();
				double doctorCost = (income * billProject.getDoctorCost()) / 100.0;
				sumIncome += income;
				sumDoctorCost += doctorCost;
			}

			// 获取本院开单的所有客户治疗信息的详细信息
			List<TreatInfo> tiList = tiDao.getClinicTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime);
			for (int j = 0; j < tiList.size(); j++) {
				TreatInfo treatInfo = tiList.get(j);
				//如果是本院
				if (treatInfo.getClinicId() == clinic.getClinicId()) {
					sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
					continue;
				}
				//如果是分院
				sumOperatorCost += (treatInfo.getUnitPrice() * treatInfo.getExcutorCost()) / 100.0;
				sumOtherClinicCost += (treatInfo.getUnitPrice() * treatInfo.getClinicCost()) / 100.0;
			}

			// 获取本院 执行者执行其他门诊单据的治疗信息
			List<TreatInfo> tiList2 = tiDao.getOtherTreatInfo(clinic.getClinicId(),
					project.getProjectId(), startTime, endTime, 1);
			for (int j = 0; j < tiList2.size(); j++) {
				TreatInfo ti = tiList2.get(j);
				sumOperatorCost += (ti.getUnitPrice() * ti.getExcutorCost() / 100);
				sumOtherClinicIncome += (ti.getUnitPrice() * ti.getClinicCost() / 100);
			}
			treatProfit += (sumIncome+sumOtherClinicIncome-sumDoctorCost-sumOperatorCost-sumOtherClinicCost);
		}
		JSONObject resultJO = new JSONObject();
		resultJO.put("clinicId", clinic.getClinicId());
		resultJO.put("clinicName", clinic.getName());
		resultJO.put("projectId", 0);
		resultJO.put("projectName", "治疗项目");
		resultJO.put("type", "收入");
		resultJO.put("price", DoubleUtil.parseDoubleTwo(treatProfit));
		resultJA.add(resultJO);
		
		return resultJA;
	}

	@Override
	public JSONArray getClinicFundProjectDetail(Clinic clinic, int projectId, long startTime,
			long endTime) {
		//[operatorId,operatorCode,operatorName,price,dateTime]
		JSONArray resultJA = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<BillProject> bpList = bDao.getFundBPSummary(clinic.getClinicId(), projectId, startTime, endTime);
		for (int i = 0; i < bpList.size(); i++) {
			BillProject bp = bpList.get(i);
			Bill bill = bDao.findById(bp.getId().getBillId());
			Employee operator = eDao.findById(bill.getOperatorId());
			
			JSONObject resultJO = new JSONObject();
			resultJO.put("operatorId", operator.getEmployeeId());
			resultJO.put("operatorCode", operator.getEmployeeCode());
			resultJO.put("operatorName", operator.getName());
			resultJO.put("price", bp.getPrice());
			resultJO.put("dateTime", sdf.format(bill.getDatetime()));
			resultJA.add(resultJO);
		}
		return resultJA;
	}

	@Override
	public Bill getClinicFirstBill(Clinic clinic) {
		return bDao.getClinicFirstBill(clinic.getClinicId());
	}

	@Override
	public JSONArray getBill(Timestamp date1, Timestamp date2, String category) {
		// TODO Auto-generated method stub
		JSONArray resultJA = new JSONArray();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<Bill> blist = bDao.getBillData(date1, date2, category);
		for(int i = 0; i < blist.size(); i++){
			JSONObject resultOJ = new JSONObject();
			
			Client client = clientDao.findById(blist.get(i).getClientId());
			Employee employee = eDao.findById(blist.get(i).getOperatorId());
			Employee employee2 = eDao.findById(blist.get(i).getDoctorId());
			Department department = dpmDao.findById(blist.get(i).getDepartmentId());
			Clinic clinic = cDao.findById(blist.get(i).getClinicId());
			
			resultOJ.put("billId", blist.get(i).getBillId());
			resultOJ.put("clientName", client.getName());
			resultOJ.put("operatorName", employee.getName());
			resultOJ.put("doctorName", employee2.getName());
			resultOJ.put("departmentName", department.getName());
			resultOJ.put("clinicName", clinic.getName());
			resultOJ.put("totalCost", blist.get(i).getTotalCost());
			resultOJ.put("totalPrice", blist.get(i).getTotalPrice());
			resultOJ.put("dateTime", sdf.format(blist.get(i).getDatetime()));
			if(blist.get(i).getStatus() == 0){
				resultOJ.put("status", "已支付");
			}else if(blist.get(i).getStatus() == 1){
				resultOJ.put("status", "未支付");
			}else if(blist.get(i).getStatus() == -1){
				resultOJ.put("status", "已作废");
			}
			resultJA.add(resultOJ);
		}
		return resultJA;
	}

}
